/**
 * Bishop.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

import java.lang.Math;

public class Bishop extends Piece {
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes an empty bishop.
 	 */
	public Bishop() {
		super();
	}

	/**
	 * Initializes a new bishop of a given color.
	 * 
	 * @param white true if the bishop must be white, false if it must be black.
	 */
	public Bishop(boolean white) {
		super(white);
	}
	
	/**
	 * Initializes a new bishop with all of its attributes.
	 * 
	 * @param x	the x coordinate of the bishop on the chessboard.
	 * @param y the y coordinate of the bishop on the chessboard.
	 * @param white true if the bishop must be white, false if it must be black.
	 */
	public Bishop(int x, int y, boolean white) {
		super(x, y, white);
	}
	
	/**
	 * Copy constructor.
	 * Initializes a new bishop from a given one.
	 * 
	 * @param other the bishop to copy in the new one.
	 */
	public Bishop(Bishop other) {
		super(other);
	}
	
	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------

	/**
	 * Checks if a move is valid for this kind of piece.
	 * 
	 * @param cb the chessboard.
	 * @param x	the x coordinate where we want the bishop to move on the chessboard.
	 * @param y the y coordinate where we want the bishop to move on the chessboard.
	 * @param act true if we want to remove the piece that can be taken.
	 * 
	 * @return true if the move is valid.
	 */
	public boolean validMove(Chessboard cb, int x, int y, boolean act)
	{
		boolean isValid = (cb.isOutOfRange(x, y) && differentPosition(x, y));
		int i, j;
		
		// Specific to this piece :
		if(isValid && !(Math.abs(x - getX()) == Math.abs(y - getY()))) isValid = false;
		
		// This will check if the path is clear:
		if(x > getX()) {	// The piece tries to the right.
			if(y > getY()) {	// Up and to the right.
				i = getX() + 1;
				j = getY() + 1;
				
				while(i < x && j < y && isValid) {
					if(cb.getPiece(i, j) != null) isValid = false;
					i++;
					j++;
				}
			}
			else if(y < getY()) {	// Down and to the right.
				i = getX() + 1;
				j = getY() - 1;
				
				while(i < x && j > y && isValid) {
					if(cb.getPiece(i, j) != null) isValid = false;
					i++;
					j--;
				}
			}
		} else if (x < getX()) {	// The piece tries to move to the left.
			if(y > getY()) {	// Up and to the left.
				i = getX() - 1;
				j = getY() + 1;
				
				while(i > x && j < y && isValid) {
					if(cb.getPiece(i, j) != null) isValid = false;
					i--;
					j++;
				}
			}
			else if(y < getY()) {
				i = getX() - 1;
				j = getY() - 1;
				
				while(i > x && j > y && isValid) {
					if(cb.getPiece(i, j) != null) isValid = false;
					i--;
					j--;
				}
			}
		}
		
		if(isValid && cb.getPiece(x, y) != null && act)	// If there is already a piece here, it can now be taken:
		{
			if(cb.getPiece(x, y).isWhite() != isWhite()) cb.removePiece(x, y);	// The piece can be taken.
			else isValid = false;
		}
		
		return isValid;
	}

	/**
	 * Returns the character associated to the type of this piece.
	 * 
	 * @return the character associated to the type of this piece 
	 */
	public char getType()
	{
		if(isWhite()) return 'B';
		else return 'b';
	}
	
	/**
	 * Returns a copy of this piece.
	 * 
	 * @return a copy of this piece.
	 */
	public Piece getCopy() {
		Bishop copy = new Bishop(m_x, m_y, m_white);
		
		return copy;
	}
	
	//-----------------------------------------------------------------------------------------
}
